<template>
    <div>
        <ul class="nav nav-tabs" role="tablist" style="margin-bottom:5px;">
            <li role="presentation" class="active"><a href="#bf_manage" role="tab" data-toggle="tab">报废管理</a></li>
            <li role="presentation"><a href="#bfwcl" role="tab" data-toggle="tab">申请报废未处理</a></li>
            <li role="presentation"><a href="#bfstorage" role="tab" data-toggle="tab">报废品库</a></li>
            <li role="presentation"><a href="#bfsellout" role="tab" data-toggle="tab">报废卖出单</a></li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane active" id="bf_manage">
                <table id="assetsTable" class="table"></table>
                <div class="modal fade" id="gdzc_discard_modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title">填写报废信息</h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-group1">
                                    <label for="">报废原因</label>
                                    <textarea id="bfreason" type="text" class="form-control input-sm"></textarea>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" @click="bfSubmit">确认</button>
                            </div>
                        </div>
                                    <!-- /.modal-content -->
                    </div>
                                <!-- /.modal-dialog -->
                </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="bfwcl">
                <table id="bfwclTable" class="table"></table>
                     <div class="modal fade" id="check_submit_modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                <h4 class="modal-title">报废确认</h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-group1">
                                    <label for="">报废意见</label>
                                    <textarea class="input-sm form-control" id="bfyj"></textarea>
                                </div>
                                <div class="form-group1">
                                    <label for="">备注</label>
                                    <textarea id="beizhu" class="form-control input-sm"></textarea>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" @click="discardAgree2" id="check_submit">确定</button>
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                    <!-- /.modal-dialog -->
                </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="bfstorage">
                <table id="bfStoreTable" class="table"></table>
                <div class="modal fade" id="scrap_sellout_modal">
                            <div class="modal-dialog model-lg">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title">填写卖出信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group1">
                                            <label for="">卖出价格</label>
                                            <input id="fvalue" type="number" class="form-control input-sm">
                                        </div>
                                        <div class="form-group1">
                                            <label for="">回收单位信息</label>
                                            <input id="selltowhere" type="text" class="form-control input-sm">
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" @click="sellout" id="scrap_sellout_submit">确认</button>
                                    </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>
            </div>
            <div role="tabpanel" class="tab-pane" id="bfsellout">
                <table id="bfSelloutTable" class="table"></table>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                currentAssetsId: '',
                currentScropId: ''
            }
        },
        methods: {
            getAssetsTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/fetchassetsbydept',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'extra.selectfield': '*',
                        'extra.sortfield': 'fidno',
                        'extra.pagesize': 99999999,
                        'extra.pageindex': 1,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },

                    success: function(retdata) {
                        var operateEvents = {
                            'click a[name="sqbf"]': function(elem, value, row, index) {
                                _this.currentAssetsId = row.fidno
                                _this.showBfReasonModal()

                            }
                        }
                        $('#assetsTable').bootstrapTable('destroy')
                        $('#assetsTable').bootstrapTable({
                            columns: [{
                                field: 'fidno',
                                title: '资产编号',
                                halign: 'center'
                            }, {
                                field: 'fbarcode',
                                title: '资产编码',
                                halign: 'center'
                            }, {
                                field: 'fname',
                                title: '资产名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'ftype',
                                title: '类型',
                                halign: 'center'
                            }, {
                                field: "funitid",
                                title: '单位',
                                halign: 'center'
                            }, {
                                field: 'fvalue',
                                title: '价格',
                                halign: 'center'
                            }, {
                                field: 'savefdname',
                                title: '保管部门',
                                halign: 'center'
                            }, {
                                field: 'usedeptname',
                                title: '使用部门',
                                halign: 'center'
                            }, {
                                field: 'saveusername',
                                title: '保管人',
                                halign: 'center'
                            }, {
                                field: 'operate',
                                title: '操作',
                                halign: 'center',
                                formatter: '<a class="btn btn-theme btn-xs" name="sqbf">申请报废</a>',
                                events: operateEvents
                            }],
                            data: retdata.Table2,
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100, 200, 500],
                            smartDisplay: true,
                            // selectItemName: '',
                            showHeader: true,
                            maintainSelected: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                        })
                    }
                });
            },
            showBfReasonModal() {
                $('#gdzc_discard_modal').modal('show')
            },
            bfSubmit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/bfapply',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentAssetsId,
                        'fstatus': '已申请报废',
                        'bfreason': $('#bfreason').val(),
                        'bfapplyuser': localStorage.userid,
                        'fteamid': localStorage.activeteamid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#gdzc_discard_modal').modal('hide')
                        _this.getAssetsTable()
                    }
                });
            },
            getBfwclTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/fetchapplydiscard',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },

                    success: function(retdata) {
                        function formatter1(value, row, index) {
                            if (row.fstatus == '初审核通过') {
                                return '<span>初审通过</span>'
                            } else {
                                return '<a class = "btn btn-success btn-xs" name = "firstAgree" > 同意 </a> <a class="btn btn-danger btn-xs" name="firstReject">驳回</a > '
                            }
                        }

                        function formatter2(value, row, index) {
                            if (row.fstatus == '初审核通过') {
                                return '<a class = "btn btn-success btn-xs" name = "secondAgree" > 同意 </a> <a class="btn btn-danger btn-xs" name="secondReject">驳回</a > '
                            } else {
                                return '<span>等待初审通过</span>'
                            }
                        }
                        var operateEvents = {
                            'click a[name="firstAgree"]': function(event, value, row, index) {
                                _this.currentAssetsId = row.fidno
                                _this.discardAgree1()
                            },
                            'click a[name="firstReject"]': function(event, value, row, index) {
                                _this.currentAssetsId = row.fidno
                                _this.discardAgree2()

                            },
                            'click a[name="secondAgree"]': function(event, value, row, index) {
                                _this.currentAssetsId = row.fidno
                                _this.showDiscardAgreeModal()
                            },
                            'click a[name="secondReject"]': function(event, value, row, index) {
                                _this.currentAssetsId = row.fidno
                                _this.discardReject2()
                            }
                        }
                        $('#bfwclTable').bootstrapTable('destroy')
                        $('#bfwclTable').bootstrapTable({
                            columns: [{
                                field: 'fidno',
                                title: '资产编号',
                                halign: 'center'
                            }, {
                                field: 'fbarcode',
                                title: '资产编码',
                                halign: 'center'
                            }, {
                                field: 'fname',
                                title: '资产名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'ftype',
                                title: '类型',
                                halign: 'center'
                            }, {
                                field: 'fvalue',
                                title: '价格',
                                halign: 'center'
                            }, {
                                field: 'savefdname',
                                title: '保管部门',
                                halign: 'center'
                            }, {
                                field: 'saveusername',
                                title: '保管人',
                                halign: 'center'
                            }, {
                                field: 'fstatus',
                                title: '状态',
                                halign: 'center'
                            }, {
                                field: 'firstCheck',
                                title: '初审',
                                halign: 'center',
                                formatter: formatter1,
                                events: operateEvents,

                            }, {
                                field: 'secondCheck',
                                title: '复审',
                                halign: 'center',
                                formatter: formatter2,
                                events: operateEvents,

                            }],
                            data: retdata.Table2,
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100, 200, 500],
                            smartDisplay: true,
                            // selectItemName: '',
                            showHeader: true,
                            maintainSelected: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                        })
                    }
                });
            },
            discardAgree1() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/bfapply',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentAssetsId,
                        'fstatus': '初审核通过',
                        'fteamid': localStorage.activeteamid,
                        'fudtype': '1'
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        _this.getBfwclTable()
                    }
                });
            },
            showDiscardAgreeModal() {
                $('#check_submit_modal').modal('show')
            },
            discardAgree2() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/bfapply',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentAssetsId,
                        'fstatus': '已报废',
                        'fteamid': localStorage.activeteamid,
                        'fudtype': '2',
                        'bfsuggest': $('#bfyj').val(),
                        'bfnotes': $('#beizhu').val()
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $.ajax({
                            type: 'POST',
                            url: 'http://192.168.1.66:808/api/scrap/add',
                            data: {
                                'token': localStorage.token,
                                'userid': localStorage.userid,
                                'fidno': _this.currentAssetsId,
                            },
                            dataType: 'json',
                            crossDomain: true,
                            async: true,
                            error: function(request) {
                                _this.$store.commit('showModal','连接错误')
                            },
                            success: function(retdata) {
                                $('#check_submit_modal').modal('hide')

                                _this.getBfwclTable()
                            }
                        });
                    }
                });
            },
            discardReject1() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/bfapply',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentAssetsId,
                        'fstatus': '在用',
                        'fteamid': localStorage.activeteamid,
                        'fudtype': '1'
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        _this.getBfwclTable()
                    }
                });
            },
            discardReject2() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/assets/bfapply',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentAssetsId,
                        'fstatus': '在用',
                        'fteamid': localStorage.activeteamid,
                        'fudtype': '2'
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        _this.getBfwclTable()
                    }
                });
            },
            getScropTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scrap/fetchallscrop',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        function formatter(value, row, index) {
                            if (row.by4 == '未卖出') {
                                return '<a name="sellout" class="btn btn-sm btn-theme">卖出</a>'
                            } else {
                                return '-'
                            }
                        }
                        var operateEvents = {
                            'click a[name="sellout"]': function(event, value, row, index) {
                                _this.currentScropId = row.fidno
                                $('#scrap_sellout_modal').modal('show')
                            }
                        }
                        $('#bfStoreTable').bootstrapTable('destroy')
                        $('#bfStoreTable').bootstrapTable({
                            columns: [{
                                field: 'fidno',
                                title: '资产编号',
                                halign: 'center'
                            }, {
                                field: 'fbarcode',
                                title: '资产编码',
                                halign: 'center'
                            }, {
                                field: 'fname',
                                title: '资产名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'ftype',
                                title: '类型',
                                halign: 'center'
                            }, {
                                field: 'fvalue',
                                title: '价格',
                                halign: 'center'
                            }, {
                                field: 'by4',
                                title: '状态',
                                halign: 'center'
                            }, {
                                field: 'sellout',
                                title: '卖出',
                                halign: 'center',
                                formatter: formatter,
                                events: operateEvents,
                            }],
                            data: retdata.Table2,
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100, 200, 500],
                            smartDisplay: true,
                            // selectItemName: '',
                            showHeader: true,
                            maintainSelected: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                        })
                    }
                });
            },
            sellout() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/sellout/add',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fvalue': $('#fvalue').val(),
                        'selloutwhere': $('#selltowhere').val(),
                        'fteamid': localStorage.activeteamid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {

                        $.ajax({
                            type: 'POST',
                            url: 'http://192.168.1.66:808/api/scrap/putintosellout',
                            data: {
                                'token': localStorage.token,
                                'userid': localStorage.userid,
                                'by1': retdata.Table2[0].fidno,
                                'fidno': _this.currentScropId,
                                'fteamid': localStorage.activeteamid
                            },
                            dataType: 'json',
                            crossDomain: true,
                            error: function() {
                               _this.$store.commit('showModal','连接错误')
                            },
                            success: function() {
                                _this.getScropTable()
                                $('#scrap_sellout_modal').modal('hide')

                            }

                        })

                    }
                });
            },
            getSelloutTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/sellout/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fteamid': localStorage.activeteamid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误');
                    },

                    success: function(retdata) {
                        $('#bfSelloutTable').bootstrapTable({
                            columns: [{
                                title: '序号',
                                field: 'Row',
                                halign: 'center'
                            }, {
                                title: '卖出价格',
                                field: 'fvalue',
                                halign: 'center'
                            }, {
                                title: '卖到何处',
                                field: 'selloutwhere',
                                halign: 'center'
                            }, {
                                title: '卖出日期',
                                field: 'selloutdate',
                                halign: 'center'
                            }, {
                                title: '卖出人',
                                field: 'selloutusername',
                                halign: 'center'
                            }],

                        })
                    }
                });
            }
        },
        mounted() {
            this.getAssetsTable()
            this.getBfwclTable()
            this.getScropTable()
            this.getSelloutTable()
        }
    }
</script>
<style>

</style>